﻿namespace ScrumLite.Data
{
    using System;
    using System.Linq;
    using System.Collections.Generic;
    using System.Globalization;

    public class CommitmentStore : ScrumLite.Data.ICommitmentStore
    {
        public DateTime[] RetrieveStandupDates(Guid projectId)
        {
            using (ScrumLiteDataContext context = new ScrumLiteDataContext())
            {
                IQueryable<DateTime> query = (from commitment in context.Commitments
                                              where commitment.fk_project == projectId
                                              select commitment.createdOn).Distinct().OrderByDescending(date => date);
                return query.ToArray();
            }
        }

        public Commitment[] RetrieveCommitments(DateTime standupDate, Guid projectId)
        {
            using (ScrumLiteDataContext context = new ScrumLiteDataContext())
            {
                IQueryable<Commitment> query = from commitment in context.Commitments
                                               where commitment.fk_project == projectId && commitment.createdOn == standupDate
                                               orderby commitment.createdOn descending
                                               select commitment;
                return query.ToArray();
            }
        }

        public Commitment RetrieveCommitment(Guid commitmentId)
        {
            using (ScrumLiteDataContext context = new ScrumLiteDataContext())
            {
                IQueryable<Commitment> query = from commitment in context.Commitments
                                               where commitment.id == commitmentId
                                               select commitment;
                return query.FirstOrDefault();
            }            
        }

        public Dictionary<DateTime,int> RetrieveDailyCommitmentsCount(Guid projectId,bool closed)
        {
            using (ScrumLiteDataContext context = new ScrumLiteDataContext())
            {
                var query = (from commitment in context.Commitments
                                              where commitment.fk_project == projectId
                                              where (commitment.remaining == 0) == closed
                                              group commitment by commitment.createdOn into g
                                              orderby g.Key
                                              select new { Date = g.Key, Count = g.Count() });
                return query.ToDictionary(k => k.Date, v => v.Count);
            }
        }

        public void Add(Commitment entity)
        {
            using (ScrumLiteDataContext context = new ScrumLiteDataContext())
            {
                var commitment = new Commitment()
                {
                    id = entity.id,
                    createdOn = entity.createdOn,
                    fk_project = entity.fk_project,
                    initialEstimate = entity.initialEstimate,
                    verification = entity.verification,
                    remaining = entity.remaining,
                    title = entity.title,
                    spent = entity.spent, 
                    user_createdBy = entity.user_createdBy
                };

                context.Commitments.InsertOnSubmit(commitment);
                context.SubmitChanges();
            }
        }

        public void Update(Commitment entity)
        {
            using (ScrumLiteDataContext context = new ScrumLiteDataContext())
            {
                var original = context.Commitments.SingleOrDefault(c => c.id == entity.id);
                if (original != null)
                {
                    if (entity.fk_project != Guid.Empty)
                    {
                        original.fk_project = entity.fk_project;
                    }
                    original.initialEstimate = entity.initialEstimate;
                    original.verification = entity.verification;
                    original.remaining = entity.remaining;
                    original.spent = entity.spent;
                    original.title = entity.title;
                    original.user_createdBy = entity.user_createdBy;
                    context.SubmitChanges();
                }
            }
        }

        public void Delete(Guid id)
        {
            using (ScrumLiteDataContext context = new ScrumLiteDataContext())
            {
                var original = context.Commitments.Where(c => c.id == id).FirstOrDefault();
                if (original != null)
                {
                    context.Commitments.DeleteOnSubmit(original);
                    context.SubmitChanges();
                }
            }
        }
    }
}

